package com.pktechan.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.CustAddress;
import com.pktechan.manage.entity.CustBase;
import com.pktechan.manage.service.CustAddressService;
import com.pktechan.manage.service.CustBaseService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.vo.CustVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 产品相关功能
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@Controller
@RequestMapping("custBaseController")
public class CustBaseController {

    private static final Logger logger = LoggerFactory.getLogger(CustBaseController.class);

    @Autowired
    private CustBaseService custBaseService;
    @Autowired
    private CustAddressService custAddressService;

    /**
     * 根据id查询客户信息
     * @param custId
     * @param response
     */
    @RequestMapping("getCustBaseById")
    public void getCustBaseById(@RequestParam Integer custId, HttpServletResponse response) {
        try {
            CustVO custBase = custBaseService.getCustVOById(custId);
            HtmlUtil.writerSuccessJson(response, custBase);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    @RequestMapping("deleteById")
    public void deleteById(Integer id) {
        custBaseService.deleteById(id);
    }

    @RequestMapping("saveCustBase")
    public void saveCustBase(CustBase custBase, HttpServletResponse response) {
        try {
            logger.info("添加客户信息");
            custBaseService.save(custBase);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    @RequestMapping("updateCustBase")
    public void updateCustBase(@RequestBody CustBase custBase, HttpServletResponse response) {
        try {
            logger.info("修改到客户信息 id= "+custBase.getId());
            custBaseService.update(custBase);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 客户列表
     *
     * @param custBase
     * @return
     */
    @RequestMapping("getCustBaseList")
    public void getCustBaseList(@RequestBody CustBase custBase,HttpServletResponse response) {
        try{
            logger.info("客户列表");
            PageHelper.startPage(custBase.getPageNo(), custBase.getPageSize());
            List<CustVO> list = custBaseService.getCustVOList(custBase);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }


    /**
     * 根据id查询地址
     *
     * @param id
     * @param response
     */
    @RequestMapping(value = "getCustAddressById", method = RequestMethod.POST)
    public void getCustAddressById(@RequestParam Integer id, HttpServletResponse response) {
        try {
            logger.info("根据id=" + id + "查询地址");
            CustAddress custAddress = custAddressService.getById(id);
            HtmlUtil.writerSuccessJson(response, custAddress);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 地址列表
     *
     * @param custVO
     * @param response
     */
    @RequestMapping(value = "getCustAddressList", method = RequestMethod.POST)
    public void getProductChannelFeeList(@RequestBody CustVO custVO, HttpServletResponse response) {
        try {
            logger.info("地址列表");
            PageHelper.startPage(custVO.getPageNo(), custVO.getPageSize());
            List<CustAddress> list = custAddressService.getCustAddressList(custVO);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到客户地址
     *
     * @param custAddress
     * @param response
     */
    @RequestMapping(value = "saveCustAddress", method = RequestMethod.POST)
    public void saveCustAddress(@RequestBody CustAddress custAddress, HttpServletResponse response) {
        try {
            logger.info("添加到客户地址");
            custAddressService.save(custAddress);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改客户地址
     *
     * @param custAddress
     * @param response
     */
    @RequestMapping(value = "updateCustAddressById", method = RequestMethod.POST)
    public void updateCustAddressById(@RequestBody CustAddress custAddress, HttpServletResponse response) {
        try {
            logger.info("根据id=" +custAddress.getId() + "修改地址信息表");
            custAddressService.update(custAddress);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id删除地址信息表
     *
     * @param id
     * @param response
     */
    @RequestMapping(value = "deleteShopCartById", method = RequestMethod.POST)
    public void deleteShopCartById(@RequestParam Integer id, HttpServletResponse response) {
        try {
            logger.info("根据id=" + id + "删除地址信息表");
            custAddressService.deleteById(id);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 打开客户列表页面
     * @return
     */
    @RequestMapping("toCustBaseList")
    public String toCustBaseList() {
        return "custBase/custBaseList";
    }
    /**
     * 打开客户地址列表页面
     * @return
     */
    @RequestMapping("toCustAddressList")
    public String toCustAddressList() {
        return "custBase/custAddressList";
    }
    /**
     * 打开客户编辑页面
     * @return
     */
    @RequestMapping("toCustBaseupdate")
    public String toCustBaseupdate() {
        return "custBase/custBaseupdate";
    }
}
